﻿using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace configstore.FileSystem
{
    public class PathTool
    {
        internal string _Path { get; set; }
        internal DirectoryInfo Directory { get; set; }
        public PathTool(string path)
        {
            this._Path = path;
            this.Directory = new DirectoryInfo(path);
        }

        public System.Collections.Generic.IEnumerable<FileInfo> ReadFiles(params string[] exts)
        {
            foreach (var file in Directory.GetFiles("*", SearchOption.AllDirectories))
            {
                if (file.Name[0] == '~') continue;
                if (exts.Contains(file.Extension.Trim('.')))
                {
                    yield return file;
                }
            }
        }
        public System.Collections.Generic.IEnumerable<DirectoryInfo> ReadDirectories()
        {
            foreach (var dir in Directory.GetDirectories("*"))
            {
                yield return dir;
            }
        }
        public System.Collections.Generic.IEnumerable<FileInfo> ReadAllFiles()
        {
            foreach (var file in Directory.GetFiles("*", SearchOption.AllDirectories))
            {
                if (file.Name[0] == '~') continue;
                yield return file;
            }
        }
        public static long ReadFileLength(string path)
        {
            var fileInfo = new FileInfo(path);
            return fileInfo.Length;
        }

        public static string ToRelativePath(string path, string parent_path)
        {
            var p1 = Path.GetFullPath(path);
            var p2 = Path.GetFullPath(parent_path).TrimEnd(Path.DirectorySeparatorChar);
            
            return p1.Replace(p2, "").TrimStart(Path.DirectorySeparatorChar);
        }
    }
}
